#include "Library_t.h"
#include "Book_t.h"
#include "Borrower_t.h"
#include <limits>

int main(int argc, char *argv){
	/*Person p(string("Ahia"));
	Person q(string("Victor"));
	Person a(string("Author"));
	Book b(string("Book"),&a);
	Book d(string("Dook"),&a);
	Library_t *lib = new Library_t();
	Borrower_t *br1 = new Borrower_t(&p,301416418);
	Borrower_t *br2 = new Borrower_t(&q,111111111);
	Book_t *bo1 = new Book_t(b,string("123456789"),10);
	Book_t *bo2 = new Book_t(d,string("888"),1);
	lib->Add(string("123456789"),bo1);
	lib->Add(301416418,br1);
	lib->Add(string("888"),bo2);
	lib->Add(111111111,br2);
	lib->Borrow(string("123456789"),301416418);
	lib->Borrow(string("888"),301416418);
	lib->Borrow(string("123456789"),111111111);
	lib->Borrow(string("888"),111111111);
	//lib->RemoveBorrower(111111111);
	lib->BooksReport();
	lib->BorrowesReport();
	lib->Return(string("888"),301416418);
	lib->BooksReport();
	lib->BorrowesReport();
	delete lib;
	delete br1;
	delete br2;
	delete bo1;
	delete bo2;
	getchar();*/
	Library_t *lib = new Library_t();
	cout << "Welcome to the library!" << endl;
	bool cont = true;
	unsigned char c;
	while(cont){
		cout	<< endl 
				<< "	0: Add new borrower"							    << endl
				<< "	1: Search for a borrower"							<< endl
				<< "	2: Remove a borrower"								<< endl
				<< "	3: Add new book"							    	<< endl
				<< "	4: Search for a book"			   					<< endl
				<< "	5: Remove a book"						            << endl
				<< "	6: Register a borrow of a book" 					<< endl
				<< "	7: Register a return of a book"						<< endl
				<< "	8: Print books report"					         	<< endl
				<< "	9: Print borrowers report"					      	<< endl
				<< "        e: Exit "                                           << endl
				<< "Enter your choice: " << endl;
		cin.width(1);
		cin >> c;
		if(c == 'e'){
			delete(lib);
			break;
		}
		if(c < '0' || c > '9'){
			cout << endl << "Please enter a valid choice" << endl;
			continue;
		}
		switch(c){
			case '0':
				{
					cin.width(20);
					cout << "Enter a name:" << endl;
					string name;
					cin.width(10);
					cin.clear();
					cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
					std::getline(cin,name);
					//cin >> name;
					cout << endl << "Enter an ID:"<< endl;
					unsigned int id;
					cin.width(9);
					cin >> id;
					Person* p = new Person(name);
					Borrower_t* borrower = new Borrower_t(p,id);
					bool res = lib->AddBorrower(id,borrower);
					if(!res) {
						cout << endl << "Failed: Borrower with same ID already exists!" << endl;
						continue;
					}
					cout << endl << "New borrower was added" << endl;					
				}
				break;
			case '1':
				{
					cout << "Enter an id for search:" << endl;
					unsigned int id;
					cin.width(9);
					cin >> id;
					Borrower_t* borrower = lib->SearchBorrower(id);
					if(borrower == 0){
						cout << "Borrower with this id doesnt exist" << endl;
						continue;
					}
					cout << "Borrower was found:" << endl << *borrower;
				}
				break;
			case '2':
				{
					cout << "Enter an id to remove:" << endl;
					unsigned int id;
					cin.width(9);
					cin >> id;
					Borrower_t* borrower = 0;
					try{
						borrower = lib->RemoveBorrower(id);
					}catch(LibraryException& e){
						cout << "Failed to remove a borrower" << endl;
						continue;
					}
					if(borrower == 0){
						cout << "Borrower with this id doesnt exist" << endl;
						continue;
					}
					cout << "Borrower was removed:" << endl << *borrower;
				}
				break;
			case '3':
				{
					cout << "Enter a book name: " << endl;
					string bookName;
					//cin >> bookName;
					cin.width(30);
					cin.clear();
					cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
					std::getline(cin,bookName);
					cout << endl << "Enter an author name: " << endl;
					string authorName;
					//cin >> authorName;
					cin.width(24);
					cin.clear();
					cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
					std::getline(cin,authorName);
					Person* author = new Person(authorName);
					Book* book = new Book(bookName,author);
					cout << endl << "Enter the ISBN for the book: " << endl;
					ISBN isbn;
					cin.width(13);
					cin >> isbn;
					cout <<  endl << "Enter number of copies: " << endl;
					int numOfCopies;
					cin >> numOfCopies;
					Book_t* libBook = new Book_t(*book,isbn,numOfCopies);	
					if(lib->AddBook(isbn,libBook))
						cout << endl << "New book was added" << endl;
					else
						cout << endl << "Failed to add a book" << endl;
				}
				break;
			case '4':
				{
					cout << "Enter an ISBN for search:" << endl;
					ISBN isbn;
					cin.width(13);
					cin >> isbn;
					Book_t* book = lib->SearchBook(isbn);
					if(book == 0){
						cout << "Book with this ISBN doesnt exist" << endl;
						continue;
					}
					cout << "Book was found:" << endl << *book;
				}
				break;
			case '5':
				{
					cout << "Enter an ISBN to remove:" << endl;
					ISBN isbn;
					cin.width(13);
					cin >> isbn;
					Book_t* book = 0;
					try{
						book  = lib->RemoveBook(isbn);
					}catch(LibraryException& e){
						cout << "Failed to remove book from library, borrowers have copies of it" << endl;
						continue;
					}
					if(book == 0){
						cout << "Book with this isbn doesnt exist" << endl;
						continue;
					}
					cout << "Book was removed:" << endl << *book;
				}
				break;
			case '6':
					{
						cout << "Enter an ISBN of the book:" << endl;
						ISBN isbn;
						cin.width(13);
						cin >> isbn;
						cout << "Enter the borrower's ID:" << endl;
						unsigned int id;
						cin >> id;
						Book_t* book = lib->Borrow(isbn,id);
						if(book == 0){
							cout << "Failed to borrow the book" << endl;
							continue;
						}
						cout << "Book was borrowed" << endl;
					}
				break;
			case '7':
				{
					cout << "Enter an ISBN of the book:" << endl;
					cin.width(13);
					ISBN isbn;
					cin >> isbn;
					cout << "Enter the borrower's ID:" << endl;
					unsigned int id;
					cin >> id;
					bool res = lib->Return(isbn,id);
					if(!res ){
						cout << "Failed to return the book" << endl;
						continue;
					}
					cout << "Book was returned" << endl;
				}
				break;
			case '8':
				{
					lib->BooksReport();
				}
				break;
			case '9':
				{
					lib->BorrowesReport();
				}
				break;
			default:
			{
				cont = false;
			}
			break;
		}	
	}
	return 0;
}